package paidaxing.blog.comment.controller;

import cn.dev33.satoken.stp.StpUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import paidaxing.blog.comment.dto.LikeDTO;
import paidaxing.blog.comment.dto.request.LikeRequestDTO;
import paidaxing.blog.comment.service.LikeService;
import paidaxing.blog.common.result.Result;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/likes")
public class LikeController {
    @Resource
    private LikeService likeService;

    /**
     * 点赞/取消点赞
     *
     * @param likeRequestDTO 点赞请求DTO
     * @return 点赞结果
     */
    @PostMapping
    public Result<String> toggleLike(@RequestBody LikeRequestDTO likeRequestDTO) {
        if (log.isInfoEnabled()) {
            log.info("点赞操作: {}", likeRequestDTO);
        }
        boolean result = likeService.toggleLike(likeRequestDTO);
        if (!result) {
            return Result.failed("操作失败");
        }
        return Result.success("操作成功!");
    }

    /**
     * 获取点赞状态
     *
     * @param targetType 目标类型
     * @param targetId 目标ID
     * @return 是否已点赞
     */
    @GetMapping("/status")
    public Result<Boolean> getLikeStatus(@RequestParam String targetType,
                                         @RequestParam Long targetId) {
        if (log.isInfoEnabled()) {
            log.info("获取点赞状态，目标类型: {}, 目标ID: {}", targetType, targetId);
        }
        // 检查用户是否登录，未登录用户默认返回未点赞状态
        if (!StpUtil.isLogin()) {
            return Result.success("获取点赞状态成功!", false);
        }
        // 从Satoken中获取当前用户ID
        Long userId = StpUtil.getLoginIdAsLong();
        boolean status = likeService.getLikeStatus(userId, targetType, targetId);
        return Result.success("获取点赞状态成功!", status);
    }

    /**
     * 获取文章点赞列表
     *
     * @param postId 文章ID
     * @return 点赞列表
     */
    @GetMapping("/posts/{postId}/likes")
    public Result<List<LikeDTO>> getLikesByPostId(@PathVariable Long postId) {
        if (log.isInfoEnabled()) {
            log.info("获取文章点赞列表，文章ID: {}", postId);
        }
        List<LikeDTO> likes = likeService.getLikesByPostId(postId);
        return Result.success("获取文章点赞列表成功!", likes);
    }

    /**
     * 获取评论点赞列表
     *
     * @param commentId 评论ID
     * @return 点赞列表
     */
    @GetMapping("/comments/{commentId}/likes")
    public Result<List<LikeDTO>> getLikesByCommentId(@PathVariable Long commentId) {
        if (log.isInfoEnabled()) {
            log.info("获取评论点赞列表，评论ID: {}", commentId);
        }
        List<LikeDTO> likes = likeService.getLikesByCommentId(commentId);
        return Result.success("获取评论点赞列表成功!", likes);
    }

    /**
     * 获取目标点赞数
     *
     * @param targetType 目标类型
     * @param targetId 目标ID
     * @return 点赞数
     */
    @GetMapping("/count")
    public Result<Integer> getLikeCount(@RequestParam String targetType,
                                        @RequestParam Long targetId) {
        if (log.isInfoEnabled()) {
            log.info("获取点赞数，目标类型: {}, 目标ID: {}", targetType, targetId);
        }
        Integer count = likeService.getLikeCount(targetType, targetId);
        return Result.success("获取点赞数成功!", count);
    }
}